using Lzfy_His_Service.Models;
using Lzfy_His_Service.Services;
using Microsoft.AspNetCore.Mvc;

namespace Lzfy_His_Service.Controllers
{
    /// <summary>
    /// 退费任务操作日志控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class RefundTaskOpLogController : ControllerBase
    {
        private readonly IRefundTaskOpLogService _opLogService;
        private readonly ILogger<RefundTaskOpLogController> _logger;

        public RefundTaskOpLogController(IRefundTaskOpLogService opLogService, ILogger<RefundTaskOpLogController> logger)
        {
            _opLogService = opLogService;
            _logger = logger;
        }

        /// <summary>
        /// 创建操作日志
        /// </summary>
        /// <param name="request">操作日志请求</param>
        /// <returns>操作结果</returns>
        [HttpPost("create")]
        public async Task<IActionResult> CreateOpLog([FromBody] CreateOpLogRequest request)
        {
            try
            {
                // 参数验证
                if (request.TaskDetailId <= 0)
                {
                    return BadRequest(new { success = false, message = "任务详情ID不能为空或小于等于0" });
                }

                if (string.IsNullOrWhiteSpace(request.OpName))
                {
                    return BadRequest(new { success = false, message = "操作名称不能为空" });
                }

                if (string.IsNullOrWhiteSpace(request.Username))
                {
                    return BadRequest(new { success = false, message = "用户账号不能为空" });
                }

                if (string.IsNullOrWhiteSpace(request.Fullname))
                {
                    return BadRequest(new { success = false, message = "用户全名不能为空" });
                }

                // 字段长度验证
                if (request.OpName.Length > 20)
                {
                    return BadRequest(new { success = false, message = "操作名称长度不能超过20个字符" });
                }

                if (request.Username.Length > 32)
                {
                    return BadRequest(new { success = false, message = "用户账号长度不能超过32个字符" });
                }

                if (request.Fullname.Length > 32)
                {
                    return BadRequest(new { success = false, message = "用户全名长度不能超过32个字符" });
                }

                if (!string.IsNullOrWhiteSpace(request.ActionId) && request.ActionId.Length > 50)
                {
                    return BadRequest(new { success = false, message = "ActionID长度不能超过50个字符" });
                }

                var result = await _opLogService.CreateOpLogAsync(request);

                if (result)
                {
                    return Ok(new { success = true, message = "操作日志创建成功" });
                }
                else
                {
                    return StatusCode(500, new { success = false, message = "操作日志创建失败" });
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "创建操作日志时发生异常");
                return StatusCode(500, new { success = false, message = "服务器内部错误" });
            }
        }
    }
}